﻿using FangChanCrm.Services.Interface;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Maticsoft.Model;
using System.Reflection;
using BLL;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using FangChanCrmEF;

namespace FangChanCrm.Controllers
{
    public class BuMenController : Controller
    {
        private readonly It_BuMenBiaoService t_BuMenBiaoService;
        private readonly It_GongSiBiaoService t_GongSiBiaoService;
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;

        public BuMenController(It_BuMenBiaoService buMenBiaoService, It_GongSiBiaoService gongSiBiaoService, It_RenYuanBiaoService renYuanBiaoService, IConfiguration configuration)
        {
            t_BuMenBiaoService = buMenBiaoService;
            t_GongSiBiaoService = gongSiBiaoService;
            t_RenYuanBiaoService = renYuanBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public IActionResult BuMenAdd()
        {
            HttpContext.Request.Cookies.TryGetValue("账户", out string value);
            return LoginSession.ReturnBoolSessionAndCookie(HttpContext.Session.GetString("loginid"), value, View("../Login/Login"), View("../Login/LoginChongXin"), View());
        }
        public IActionResult BuMenList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult BuMenXiuGai()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public string TianJiaBuMen()
        {
            string BuMenMingCheng = Request.Form["ipt_BuMenMingCheng"];
            string slt_GongSiMingCheng = Request.Form["slt_GongSiMingCheng"];
            string BuMenMeShi = Request.Form["BuMenMeShi"];
            t_BuMenBiao t_BuMenBiao = new t_BuMenBiao();
            t_BuMenBiao.BuMenMingCheng = BuMenMingCheng;

            t_BuMenBiao.GongSiId = Convert.ToInt32(slt_GongSiMingCheng);
            t_BuMenBiao.ZhanQuZhuangTai = BuMenMeShi;
            t_BuMenBiao.ZhuangTai = "启用";
            string shuju = t_BuMenBiaoService.BuMenBiaoAdd(t_BuMenBiao);
            return shuju;
        }
        public string BangDingGongSi()
        {
            List<t_GongSiBiao> list = t_BuMenBiaoService.BangDingGongSi();
            string json = "[";
            for (int i = 0; i < list.Count; i++)
            {
                json += "{";
                json += "\"GongSiMingChenhg\":\"" + list[i].GongSiMingCheng + "\",\"Id\":\"" + list[i].Id + "\"";
                json += "}";
                if (i != list.Count - 1)
                {
                    json += ",";
                }
            }
            json += "]";
            return json;
        }
        public string BangDingRenYuan()
        {
            List<t_RenYuanBiao> list = t_BuMenBiaoService.BangDingRenYuan();
            string json = "[";
            for (int i = 0; i < list.Count; i++)
            {
                json += "{";
                json += "\"XingMing\":\"" + list[i].XingMing + "\",\"Id\":\"" + list[i].Id + "\"";
                json += "}";
                if (i != list.Count - 1)
                {
                    json += ",";
                }
            }
            json += "]";
            return json;
        }
        public string BuMenLB()
        {
            string ipt_BuMenMingCheng = Request.Form["ipt_BuMenMingCheng"];
            string ipt_GongSiMingCheng = Request.Form["ipt_GongSiMingCheng"];
            string slt_ZhanQuZhuangTai = Request.Form["slt_ZhanQuZhuangTai"];
            string slt_ZhuangTai = Request.Form["slt_ZhuangTai"];
            t_BuMenBiao bumen = new t_BuMenBiao();
            //bumen.BeiZhu1 = "";
            if (ipt_BuMenMingCheng != "")
            {
                bumen.BuMenMingCheng = ipt_BuMenMingCheng;
            }
            if (ipt_GongSiMingCheng != "")
            {
                if (ipt_GongSiMingCheng != "0")
                {
                    bumen.GongSiId = Convert.ToInt32(ipt_GongSiMingCheng);
                }
            }
            if (slt_ZhanQuZhuangTai != "")
            {
                if (slt_ZhanQuZhuangTai != "0")
                {
                    bumen.ZhanQuZhuangTai = slt_ZhanQuZhuangTai;
                }
            }
            if (slt_ZhuangTai != "")
            {
                if (slt_ZhuangTai != "0")
                {
                    bumen.ZhuangTai = slt_ZhuangTai;
                }
            }

            //var returnstr = new List<t_BuMenBiao>();
            //returnstr = t_BuMenBiaoService.GetList().Where(ReturnWhere(bumen)).ToList();
            string shuju = t_BuMenBiaoService.GetListWithFenYe(bumen, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
            //string shu = Helper.ListToJsonAddFenYe(returnstr, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
            if (t_BuMenBiaoService.listshuju().ToList().Count != 0)
            {
                return shuju;
            }
            else
            {
                string jsonString = "{\"draw\":1,\"total\":0,\"total\":0,";
                jsonString += "\"" + "data" + "\":" + "[]" + ",";
                jsonString = jsonString.TrimEnd(',');
                return jsonString + "}";
                //return "[]";
            }
        }
        public string ZhuangTaiXG()
        {
            string shuju = "";
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var BuMen = _context.t_BuMenBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["BuMenId"]));
                if (BuMen.ZhuangTai == "启用")
                {
                    BuMen.ZhuangTai = "停用";
                }
                else
                {
                    BuMen.ZhuangTai = "启用";
                }
                if (_context.SaveChanges() > 0)
                {
                    shuju = "success";
                }
                else
                {
                    shuju = "error";
                }
            }
            return shuju;
        }
        public string HuiBangShuJu()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            List<t_BuMenBiao> list = t_BuMenBiaoService.huibangQiYelist(id);
            string json = "";
            json += "[";
            for (int i = 0; i < list.Count; i++)
            {
                json += "{";
                json += "\"BuMenMingCheng\":\"" + list[i].BuMenMingCheng + "\",\"GongSiId\":\"" + list[i].GongSiId + "\",\"ZhanQuZhuangTai\":\"" + list[i].ZhanQuZhuangTai + "\"";
                json += "}";
                if (i != list.Count - 1)
                {
                    json += ",";
                }
            }
            json += "]";
            return json;
            //return list;
        }
        public string XiuGaiBuMen()
        {
            string shuju = "";
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var BuMen = _context.t_BuMenBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["Id"]));
                BuMen.BuMenMingCheng = Request.Form["ipt_BuMenMingCheng"].ToString();
                BuMen.GongSiId = Convert.ToInt32(Request.Form["slt_GongSiMingCheng"]);
                BuMen.ZhanQuZhuangTai = Request.Form["BuMenMeShi"].ToString();
                if (_context.SaveChanges() > 0)
                {
                    shuju = "success";
                }
                else
                {
                    shuju = "error";
                }
            }
            return shuju;
        }
    }
}
